﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SQLite;

namespace LibraryManagementSystem
{
    public partial class 注册页面 : Form
    {

        public 注册页面()
        {
            InitializeComponent();
        }

        private void 注册页面_Load(object sender, EventArgs e){ }


        //注册后返回首页
        private void registerBtn_Click(object sender, EventArgs e)
        {
            //获取注册信息
            string mail = mailTxb.Text;
            string pwd = pwdTxb.Text;

            if ( mail.Trim() == "" || pwd.Trim() == "")
            {
                MessageBox.Show("请完善注册信息!", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //检验邮箱格式
                string emailPattern = @"\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}";
                if (!Regex.Match(mail, emailPattern).Success)
                {
                    MessageBox.Show("账号格式错误！\n请输入正确的邮箱！", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                //连接数据库
                string conPath = @"Data Source = ../../test.db";
                using (SQLiteConnection con = new SQLiteConnection(conPath))
                {
                    con.Open();
                    string sql;
                    SQLiteCommand cmd;

                    //注册类型
                    if (adminRBtn.Checked == true)
                    {
                        try
                        {
                            #region 待修改
                            //检索账号是否存在
                            sql = "select * from admin where adminMail='" + mail + "'";
                            cmd = new SQLiteCommand(sql, con);
                            if (cmd.ExecuteScalar() != null)
                            {
                                MessageBox.Show("该邮箱已被注册，请直接登录。","Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            else
                            {
                                //添加进数据库
                                sql = "insert into admin values ('" + mail + "','" + pwd + "')";
                                cmd = new SQLiteCommand(sql, con);
                                int i = cmd.ExecuteNonQuery();
                                if (i == 1)
                                {
                                    MessageBox.Show("管理员" + mail + "\n注册成功！", "Congratulations");
                                    this.Close();
                                }

                            }
                            #endregion
                        }
                        catch { }

                    }
                    else
                    {
                        try
                        {
                            #region 待修改
                            //检索账号是否存在
                            sql = "select * from user where userMail='" + mail + "'";
                            cmd = new SQLiteCommand(sql, con);
                            if (cmd.ExecuteScalar() != null)
                            {
                                MessageBox.Show("该邮箱已被注册，请直接登录。", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            else
                            {
                                //添加进数据库
                                sql = "insert into user values ('" + mail + "','" + pwd + "')";
                                cmd = new SQLiteCommand(sql, con);
                                int i = cmd.ExecuteNonQuery();
                                if (i == 1)
                                {
                                    MessageBox.Show("用户" + mail + "\n注册成功！", "Congratulations");
                                    this.Close();
                                }

                            }
                            #endregion
                        }
                        catch { }
                    }
                }
                    

                
            }

            
        }

        private void pwdChk_CheckedChanged(object sender, EventArgs e)
        {
            if (pwdChk.Checked)
            {
                pwdTxb.PasswordChar = '\0';//显示文本
            }
            else
            {
                pwdTxb.PasswordChar = '*';
            }
        }
    }
}
